﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Import_Movie.xaml
    /// </summary>
    public partial class Import_Movie : UserControl
    {
        /// <summary>
        /// Reference to Parent ImportMedia()-Dialog.
        /// </summary>
        private ImportMedia ParentMediaImport { get; set; }

        /// <summary>
        /// Currently Selected MediaInfo()-Object.
        /// </summary>
        public Objects.MediaInfo SelectedMediaInfo { get; set; }

        /// <summary>
        /// Movie()-Data for the currently selected MediaInfo().
        /// </summary>
        public Objects.Movie MovieData { get; set; }

        /// <summary>
        /// Marker for any pending changes.
        /// </summary>
        public bool PendingChanges { get; set; }

        public Objects.Movie getCurrentData
        {
            get
            {
                Objects.Movie oCurrentMovieData = new Objects.Movie();
                oCurrentMovieData.AudioInfo = ctlAudioInfo.CurrentAudioInfo;
                oCurrentMovieData.Cast = ctlCast.Cast;
                oCurrentMovieData.Director = txtDirector.Text;
                if (ctlDiskMedia.SelectedDisk != null)
                    oCurrentMovieData.DiskMediaID = ctlDiskMedia.SelectedDisk.DiskMediaID;
                else
                    oCurrentMovieData.DiskMediaID = 0;

                oCurrentMovieData.Files = ctlFiles.SelectedFiles;

                if (cmbGenres.SelectedIndex > -1)
                    oCurrentMovieData.GenreID = ((Objects.Genre)cmbGenres.SelectedItem).GenreID;
                else
                    oCurrentMovieData.GenreID = 0;
                oCurrentMovieData.Name = txtTitle.Text;
                if(SelectedMediaInfo.UserdefinedMediaInfo != null && SelectedMediaInfo.UserdefinedMediaInfo.GetType() == typeof(Objects.Movie))
                {
                    Objects.Movie oMovie = (Objects.Movie)SelectedMediaInfo.UserdefinedMediaInfo;
                    oCurrentMovieData.OnlineMediaID = oMovie.OnlineMediaID;
                }

                oCurrentMovieData.Plot = ctlPlot.Plot;
                oCurrentMovieData.Subtitle = txtSubtitle.Text;
                oCurrentMovieData.VideoInfo = ctlVideoInfo.CurrentVideoInfo;
                oCurrentMovieData.HDLevel = oCurrentMovieData.VideoInfo.HDLevel;

                try
                {
                    oCurrentMovieData.Year = int.Parse(txtYear.Text);
                }
                catch (Exception)
                {
                    oCurrentMovieData.Year = 0;
                }

                return oCurrentMovieData;
            }
        }

        /// <summary>
        /// Initialize a new Import_Movie-Control.
        /// </summary>
        /// <param name="dlgParentMediaImportDialog">Reference to parent ImportMedia()-Dialog.</param>
        /// <param name="oSelectedMediaInfo">Currently selected MediaInfo()-Object.</param>
        public Import_Movie(ImportMedia dlgParentMediaImportDialog, Objects.MediaInfo oSelectedMediaInfo)
        {
            InitializeComponent();

            SelectedMediaInfo = oSelectedMediaInfo;
            ParentMediaImport = dlgParentMediaImportDialog;

            PendingChanges = false;
        }

        public Import_Movie(Objects.MediaInfo oSelectedMediaInfo)
        {
            InitializeComponent();

            SelectedMediaInfo = oSelectedMediaInfo;

            PendingChanges = false;

            // Disable integrated save-feature.
            mitSaveChanges.IsEnabled = false;
            mitSaveChanges.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// Initialize Control-Layout and Values.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // Populate Genre-Selector.
            cmbGenres.Items.Clear();
            List<Objects.Genre> lGenres = Objects.Genre.getAllGenres();
            foreach (Objects.Genre oGenre in lGenres)
            {
                cmbGenres.Items.Add(oGenre);
            }

            ctlAudioInfo.SelectedFilePath = SelectedMediaInfo.FullPath;
            ctlVideoInfo.SelectedFilePath = SelectedMediaInfo.FullPath;
            ctlFiles.SelectedFiles.Add(SelectedMediaInfo.FullPath);
            ctlFiles.setFileCount();

            // Check for saved editing-sessions.
            if (SelectedMediaInfo.UserdefinedMediaInfo != null && SelectedMediaInfo.UserdefinedMediaInfo.GetType() == typeof(Objects.Movie))
            {
                Objects.Movie oItemDetails = (Objects.Movie)SelectedMediaInfo.UserdefinedMediaInfo;
                txtTitle.Text = oItemDetails.Name;
                txtSubtitle.Text = oItemDetails.Subtitle;
                txtDirector.Text = oItemDetails.Director;
                txtYear.Text = oItemDetails.Year.ToString();

                if (oItemDetails.Plot != String.Empty)
                {
                    ctlPlot.setPlot(oItemDetails.Plot);
                    ctlPlot.setStatusImage(true);
                }

                if (oItemDetails.Cast != String.Empty)
                {
                    ctlCast.setCast(oItemDetails.Cast);
                    ctlCast.setStatusImage(true);
                }

                if (oItemDetails.Files.Count > 0)
                {
                    ctlFiles.SelectedFiles.Clear();
                    ctlFiles.SelectedFiles = oItemDetails.Files;
                    ctlFiles.setFileCount();
                    ctlFiles.setStatusIcon();
                }
                ctlAudioInfo.CurrentAudioInfo = oItemDetails.AudioInfo;
                ctlAudioInfo.loadInfo();
                ctlVideoInfo.CurrentVideoInfo = oItemDetails.VideoInfo;
                ctlVideoInfo.loadInfo();
                ctlCoverArt.setCoverImage(SelectedMediaInfo.CoverImagePath);
                ctlDiskMedia.setDiskMedia(new Objects.DiskMedia(oItemDetails.DiskMediaID));
                ctlDiskMedia.setStatusIcon();
                if (oItemDetails.DiskMediaID != 0)
                {
                    ctlDiskMedia.setDiskMedia(new Objects.DiskMedia(oItemDetails.DiskMediaID));
                }
                bool bCoverSet = false;
                if (ctlCoverArt.CoverImagePath != String.Empty)
                {
                    bCoverSet = true;
                }
                ctlCoverArt.setStatusImage(bCoverSet);

                if (oItemDetails.GenreID != 0)
                {
                    Objects.Genre oSelectedGenre = new Objects.Genre(oItemDetails.GenreID);
                    cmbGenres.SelectedItem = oSelectedGenre;
                    cmbGenres.Text = oSelectedGenre.Name;
                    //string sSelectedGenreName = cmbGenres.SelectedItem.ToString();
                }
            }
            else
            {
                // If no editing-sessions exist, import values from MediaInfo.
                txtTitle.Text = SelectedMediaInfo.FileName;
                //ctlFiles.SelectedFiles.Add(SelectedMediaInfo.FullPath);

                ctlAudioInfo.AnalyseAudio();
                ctlVideoInfo.AnalyseVideo();
            }

            PendingChanges = false;
        }

        /// <summary>
        /// User selected to save changes to current MediaInfo()-Object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mitSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            SaveChanges();
        }

        /// <summary>
        /// Save changes to current MediaInfo()-Object.
        /// </summary>
        private void SaveChanges()
        {
            if (txtTitle.Text != String.Empty && txtYear.Text != String.Empty && ctlFiles.SelectedFilesCount > 0)
            {
                MovieData = new Objects.Movie();
                MovieData.VideoInfo = ctlVideoInfo.CurrentVideoInfo;
                MovieData.AudioInfo = ctlAudioInfo.CurrentAudioInfo;
                MovieData.Name = txtTitle.Text;
                MovieData.Subtitle = txtSubtitle.Text;

                try
                {
                    if (txtYear.Text.Contains('-'))
                        txtYear.Text = txtYear.Text.Split('-')[0];
                    else if (txtYear.Text.Contains('/'))
                        txtYear.Text = txtYear.Text.Split('/')[2];

                    MovieData.Year = int.Parse(txtYear.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Die Eingabe \"" + txtYear.Text + "\" ist keine gültige Jahreszahl. Bitte geben Sie eine Zahl ein!", "Fehlerhafte Eingabe", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                MovieData.Plot = ctlPlot.Plot;
                MovieData.Cast = ctlCast.Cast;
                MovieData.Director = txtDirector.Text;
                MovieData.Files = ctlFiles.SelectedFiles;
                MovieData.HDLevel = ctlVideoInfo.CurrentVideoInfo.HDLevel;
                MovieData.GenreID = 0;
                MovieData.DiskMediaID = ctlDiskMedia.SelectedMediaID;
                if (cmbGenres.SelectedItem != null)
                {
                    Objects.Genre oSelectedGenre = (Objects.Genre)cmbGenres.SelectedItem;
                    MovieData.GenreID = oSelectedGenre.GenreID;
                }

                SelectedMediaInfo.CoverImagePath = ctlCoverArt.CoverImagePath;

                SelectedMediaInfo.UserdefinedMediaInfo = MovieData;

                if(ParentMediaImport != null)
                    ParentMediaImport.updateSelected(SelectedMediaInfo);


                //SelectedMediaInfo.NewFile = true;
                SelectedMediaInfo.IsEdited = true;
                PendingChanges = false;
            }
            else if (txtTitle.Text == String.Empty)
                MessageBox.Show("Bitte geben Sie einen Titel ein!", "Kein Titel", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            else if(txtYear.Text == String.Empty)
                MessageBox.Show("Bitte geben Sie ein Erscheinungs-Jahr ein!", "Kein Erscheinungs-Jahr", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            else if(ctlFiles.SelectedFilesCount == 0)
                MessageBox.Show("Bitte weisen Sie dem Eintrag zumindest eine Datei zu!", "Keine Dateien ausgewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        private void mitGetDetailsOnline_Click(object sender, RoutedEventArgs e)
        {
            // ADD CODE.
            Dialoges.WebData.AmazonSearch dlgAmazonMovieSearch = new Dialoges.WebData.AmazonSearch(this, txtTitle.Text);
            dlgAmazonMovieSearch.ShowDialog();
        }

        /// <summary>
        /// Set marker for pending changes...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            PendingChanges = true;
        }

        /// <summary>
        /// Set marker for pending changes...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSubtitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            PendingChanges = true;
        }

        /// <summary>
        /// Set marker for pending changes...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDirector_TextChanged(object sender, TextChangedEventArgs e)
        {
            PendingChanges = true;
        }

        /// <summary>
        /// Set marker for pending changes...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtYear_TextChanged(object sender, TextChangedEventArgs e)
        {
            PendingChanges = true;

            if (txtYear.Text.Length > 4)
                txtYear.Text = txtYear.Text.Substring(0, 4);
        }

        /// <summary>
        /// Set marker for pending changes...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbGenres_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PendingChanges = true;
        }
    }
}
